package com.yic.module.base.service.workshop;

import cn.hutool.core.util.ObjectUtil;
import com.yic.module.base.dal.dataobject.center.CenterDO;
import com.yic.module.base.dal.redis.RedisKeyConstants;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import com.yic.module.base.controller.admin.workshop.vo.*;
import com.yic.module.base.dal.dataobject.workshop.WorkshopDO;
import com.yic.framework.common.pojo.PageResult;

import com.yic.module.base.convert.workshop.WorkshopConvert;
import com.yic.module.base.dal.mysql.workshop.WorkshopMapper;

import static com.yic.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.yic.module.base.enums.ErrorCodeConstants.*;

/**
 * 生产车间 Service 实现类
 *
 * @author 系统管理员
 */
@Service
@Validated
public class WorkshopServiceImpl implements WorkshopService {

    @Resource
    private WorkshopMapper workshopMapper;

    @Override
    public Long createWorkshop(WorkshopCreateReqVO createReqVO) {
        validateCreateOrUpdate(null, createReqVO.getName(), createReqVO.getCode());
        // 插入
        WorkshopDO workshop = WorkshopConvert.INSTANCE.convert(createReqVO);
        workshopMapper.insert(workshop);
        // 返回
        return workshop.getId();
    }

    @Override
    @CacheEvict(cacheNames = RedisKeyConstants.WORKSHOP, key = "#updateReqVO.id")
    public void updateWorkshop(WorkshopUpdateReqVO updateReqVO) {
        validateCreateOrUpdate(updateReqVO.getId(), updateReqVO.getName(), updateReqVO.getCode());
        // 校验存在
        validateWorkshopExists(updateReqVO.getId());
        // 更新
        WorkshopDO updateObj = WorkshopConvert.INSTANCE.convert(updateReqVO);
        workshopMapper.updateById(updateObj);
    }

    @Override
    @CacheEvict(cacheNames = RedisKeyConstants.WORKSHOP, key = "#id")
    public void deleteWorkshop(Long id) {
        // 校验存在
        validateWorkshopExists(id);
        // 删除
        workshopMapper.deleteById(id);
    }

    private void validateCreateOrUpdate(Long id, String name, String code){
        validateNameExists(id, name);
        validateCodeExist(id, code);
    }

    private void validateNameExists(Long id, String name) {
        WorkshopDO workshop = workshopMapper.selectByName(name);
        if (ObjectUtil.isEmpty(workshop)){
            return;
        }
        if (id == null){
            throw exception(WORKSHOP_NAME_EXISTS);
        }
        if (!id.equals(workshop.getId())){
            throw exception(WORKSHOP_NAME_EXISTS);
        }
    }

    private void validateCodeExist(Long id, String code){
        WorkshopDO workshop = workshopMapper.selectByCode(code);
        if (ObjectUtil.isEmpty(workshop)){
            return;
        }
        if (id == null){
            throw exception(WORKSHOP_CODE_EXISTS);
        }
        if (!id.equals(workshop.getId())){
            throw exception(WORKSHOP_CODE_EXISTS);
        }
    }
    private void validateWorkshopExists(Long id) {
        if (workshopMapper.selectById(id) == null) {
            throw exception(WORKSHOP_NOT_EXISTS);
        }
    }

    @Override
    @Cacheable(cacheNames = RedisKeyConstants.WORKSHOP, key = "#id")
    public WorkshopDO getWorkshop(Long id) {
        return workshopMapper.selectById(id);
    }

    @Override
    public List<WorkshopDO> getWorkshopList(Collection<Long> ids) {
        return workshopMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<WorkshopDO> getWorkshopPage(WorkshopPageReqVO pageReqVO) {
        return workshopMapper.selectPage(pageReqVO);
    }

    @Override
    public List<WorkshopDO> getWorkshopList(WorkshopExportReqVO exportReqVO) {
        return workshopMapper.selectList(exportReqVO);
    }

    @Override
    public List<WorkshopDO> getWorkshopList(Collection<Long> ids, Collection<Integer> statuses) {
        return workshopMapper.selectList(ids, statuses);
    }

}
